package ru.ifmo.trafficspy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;

import ru.ifmo.trafficspy.analyzer.TrafficModel;
import ru.ifmo.trafficspy.analyzer.learning.TrafficModelFactory;
import ru.ifmo.trafficspy.common.Item;
import ru.ifmo.trafficspy.provider.Provider;
import ru.ifmo.trafficspy.provider.SimpleProvider;
import ru.ifmo.trafficspy.provider.TestProvider;

public class TrafficSpy {
	
	private ArrayList<TrafficModel> trafficModels;
	
	private TrafficSpy(String filename) throws FileNotFoundException {
		trafficModels = new ArrayList<TrafficModel>();
		Scanner in = new Scanner(new File(filename));
		while (in.hasNext()) {
			String modelFilename = in.nextLine();
			Scanner modelIn = new Scanner(new File(modelFilename));
			trafficModels.add(TrafficModel.read(modelIn));
		}
	}
	
	public void start() {
		Scanner in = new Scanner(System.in);
		ArrayList<Provider> providers = new ArrayList<Provider>();
		providers.add(new SimpleProvider(100, 10, 20));
		providers.add(new TestProvider());
		while (true) {
			System.out.println("traffic-spy by Fedor Tsarev & Dmitry Paraschenko, 2008");
			System.out.println("Analyzer mode");
			System.out.println("Models loaded:");
			for (TrafficModel tm : trafficModels) {
				 System.out.println(tm.getDescription());
			}
			System.out.println("Enter the number of provider or q for quit");
			for (int i = 0; i < providers.size(); i++) {
				System.out.println(i + ". " + providers.get(i).getDescription());
			}
			String userInput = "";
			while (userInput == null || userInput.trim().equals("")) {
				System.out.print("> ");
				userInput = in.nextLine();
			}
			if ("q".equals(userInput)) {
				System.out.println("Bye!");
				break;
			}
			int choiceNumber = -1;
			try {
				choiceNumber = Integer.parseInt(userInput);
			} catch (NumberFormatException nfe) {
				System.out.println("Bad number");
				continue;
			}
			if (choiceNumber < 0 || choiceNumber >= providers.size()) {
				System.out.println("Bad number");
				continue;
			}
			List<Item> traffic = providers.get(choiceNumber).getTraffic();
			TrafficModel tm = getMostProbableModel(traffic);
			if (tm == null) {
				System.out.println("No model matches this traffic.");
			} else {
				System.out.println("Most probable model is " + tm.getDescription());
				System.out.println("Probability of traffic is " + tm.getProbability(traffic));
			}
			System.out.println();
		}
	}
	
	// TODO: do something woth Exception
	public static void main(String[] args) throws FileNotFoundException {
		Locale.setDefault(Locale.US);
		new TrafficSpy(args[0]).start();
	}

	private TrafficModel getMostProbableModel(List<Item> traffic) {
		double bestProb = 0;
		TrafficModel res = null;
		for (TrafficModel tm : trafficModels) {
			double curProb = tm.getProbability(traffic);
			if (curProb > bestProb) {
				bestProb = curProb;
				res = tm;
			}
		}
		return res;
	}
	
}
